package com.ruoyi.business.controller;

import com.ruoyi.business.service.IWsdParentsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdParents;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 家长信息Controller
 *
 * @author ruoyi
 * @date 2024-04-14
 */
@RestController
@RequestMapping("wsd_parents")
public class WsdParentsController extends BaseController
{
    @Autowired
    private IWsdParentsService wsdParentsService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询家长信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:parents:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUser user)
    {

            SysUser sysUser = getLoginUser().getUser();
            Long schoolId = sysUser.getSchoolId();
            user.setSchoolId( schoolId );
            if (schoolId == null) {
                List<SysUser> emptyList = new ArrayList<>();
                return getDataTable(emptyList);
            }
            if (sysUser.isAdmin()){
                user.setSchoolId( null );
            }
            //user.setUserType( "11" );
            startPage();
            List<SysUser> list = userService.selectUserList(user);
            return getDataTable(list);

    }

    /**
     * 导出家长信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:parents:export')")
    @Log(title = "家长信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WsdParents wsdParents)
    {
        List<WsdParents> list = wsdParentsService.selectWsdParentsList(wsdParents);
        ExcelUtil<WsdParents> util = new ExcelUtil<WsdParents>(WsdParents.class);
        util.exportExcel(response, list, "家长信息数据");
    }

    /**
     * 获取家长信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:parents:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增家长信息
     */
    @PreAuthorize("@ss.hasPermi('business:parents:add')")
    @Log(title = "家长信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WsdParents wsdParents)
    {
        SysUser sysUser = new SysUser();
        sysUser.setStudentId( wsdParents.getStudentId() );
        sysUser.setNickName( wsdParents.getName() );
        sysUser.setPhonenumber( wsdParents.getPhone() );
        sysUser.setUserName( wsdParents.getPhone() );
        if (!userService.checkUserNameUnique(sysUser))
        {
            return error("新增'" + sysUser.getUserName() + "'失败，此电话已注册，请联系管理员！！");
        }
        SysUser user = getLoginUser().getUser();
        if (user == null) {
            return error("新增失败，用户未登录/已过期");
        }
        Long schoolId = user.getSchoolId();
        if (schoolId == null) {
            return error("新增失败，用户没有关联的学校信息");
        }
        sysUser.setSchoolId(schoolId);
        //小程序用户11
        sysUser.setUserType( wsdParents.getUserType() );
        return toAjax(wsdParentsService.insertWsdParents(sysUser));
    }

    /**
     * 修改家长信息
     */
    @PreAuthorize("@ss.hasPermi('business:parents:edit')")
    @Log(title = "家长信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WsdParents wsdParents) {
        if (wsdParents.getUserId()==null&&"11".equals( wsdParents.getUserType() )){
            return error("修改家长信息失败！！！");
        }
        SysUser user = getLoginUser().getUser();
        SysUser sysUser = new SysUser();
        sysUser.setNickName( wsdParents.getName() );
        sysUser.setPhonenumber( wsdParents.getPhone() );
        sysUser.setUserName( wsdParents.getPhone() );
        sysUser.setUpdateBy( user.getUserName() );
        sysUser.setUserId( wsdParents.getUserId() );
        if (!userService.checkUserNameUnique(sysUser))
        {
            return error("修改失败'" + sysUser.getUserName() + "'，此电话已注册，请联系管理员！！");
        }
        return toAjax(wsdParentsService.updateParents(sysUser));
    }

    /**
     * 删除家长信息
     */
    @PreAuthorize("@ss.hasPermi('business:parents:remove')")
    @Log(title = "家长信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }

}
